**********************************
* Regression tables using the main supermarket sample (Tables 3-10 & Table 12)
**********************************

cap log close _all
log using ../logs/regression_tables.txt, replace text nomsg name(log2)

log off log2

eststo clear
estimates clear
use ${datafolder}/supermarket_cid_yearmonth_did_sample_real_INR.dta, clear

// Create the pre-period * treatment interaction
gen pre3_cash_amt_pct_before = (yearmonth > ym(2016,7) & yearmonth <= ym(2016,10)) * cash_amt_pct_before

// Control for the impact of observed characteristics
merge m:1 cid using ${datafolder}/supermarket_cid_characteristics.dta, ///
	keep(match) nogenerate

foreach v of varlist spending_grp100 share_*_pre variety_*_pre {
	gen cafter_`v' = after*`v'
}

foreach v of varlist age_know_pre gender_know_pre marital_know_pre {
	gen byte cafter_`v' = after*`v'
}

// Get the info on whether the customer has used cash for large bill before
merge m:1 cid using ${datafolder}/supermarket_cash4largebill_cid.dta, keep(match) nogenerate

* Table 3: Forced Switch to Digital Payments and Its Effect on Spending
eststo clear
foreach payment in noncash mobile debit credit {
	eststo reg_`payment': reghdfe `payment'_amt_pct after_cash_amt_pct_before, ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
}

eststo reg_loglevel: reghdfe ltender_amt_month after_cash_amt_pct_before, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

log on log2
display "Table 3"
#delimit ;	
estfe reg_noncash reg_debit reg_mobile reg_credit reg_loglevel ,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_noncash reg_debit reg_mobile reg_credit reg_loglevel, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)')		
	nomtitles numbers 
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
	;
estfe reg_noncash reg_debit reg_mobile reg_credit reg_loglevel, restore;
#delimit cr
log off log2

* Table 4: Testing the Identifying Assumptions
eststo clear
eststo reg_noncash_pretrends: reghdfe noncash_amt_pct pre3_cash_amt_pct_before after_cash_amt_pct_before, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_loglevel_pretrends: reghdfe ltender_amt_month pre3_cash_amt_pct_before after_cash_amt_pct_before, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_noncash_addcontrols: reghdfe noncash_amt_pct pre3_cash_amt_pct_before after_cash_amt_pct_before cafter_*, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_loglevel_addcontrols: reghdfe ltender_amt_month pre3_cash_amt_pct_before after_cash_amt_pct_before cafter_*, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

log on log2
display "Table 4"
#delimit ;	
estfe reg_noncash_pretrends reg_loglevel_pretrends reg_noncash_addcontrols reg_loglevel_addcontrols,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_noncash_pretrends reg_loglevel_pretrends reg_noncash_addcontrols reg_loglevel_addcontrols
	, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)' "Controlling for observables = cafter*")  	  
	nomtitles numbers 
	varlabels(pre3_cash_amt_pct_before "PriorCashDependence * Previous 3 months"
		after_cash_amt_pct_before "PriorCashDependence * Post") 
	;
estfe reg_noncash_pretrends reg_loglevel_pretrends reg_noncash_addcontrols reg_loglevel_addcontrols, restore;
#delimit cr
log off log2

* Table 5: Effect of Digital Payments on Spending (OLS and IV Estimates)
eststo clear
eststo reg_loglevel_ols: reghdfe ltender_amt_month noncash_amt_pct, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
quietly capture estadd scalar r2_w = e(r2_within), replace

eststo iv_loglevel: ivreghdfe ltender_amt_month (noncash_amt_pct = after_cash_amt_pct_before), ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant savefirst
quietly capture estadd scalar r2_w = e(r2), replace
quietly capture estadd scalar fstat=e(widstat), replace
quietly matrix first=e(first)
quietly capture estadd scalar pr2 =  first[3,1], replace

log on log2
display "Table 5"
#delimit ;	
estfe reg_loglevel_ols iv_loglevel ,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_loglevel_ols iv_loglevel, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2_w fstat N_full, fmt(a %15.1fc %15.0fc) 
		labels("Within R2" "First-stage F statistic" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)')
	mtitles("OLS" "IV (2SLS)" ) numbers 
	varlabels(noncash_amt_pct "Digital payment fraction") 
	;
estfe reg_loglevel_ols iv_loglevel, restore;
#delimit cr
log off log2

* Table 6: Characteristics of Complying Consumers
// Run the first-stage regression group by group for the above-/below-median groups (Pre-Demonetization spending split by 2 groups)
eststo clear
forvalues i=1/2 {	
	eststo reg_noncash_grp`i': quietly reghdfe noncash_amt_pct after_cash_amt_pct_before ///
		if spending_grp2 == `i', ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
}

log on log2
display "Table 6 part 1"
#delimit ;	
estfe reg_noncash_grp* ,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_noncash_grp*, b(%8.3f) varwidth(40) brackets noconstant nostar
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations"))
	label  noomitted 
	indicate(`r(indicate_fe)')  nomtitles numbers   
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
	;
estfe reg_noncash_grp*, restore;
#delimit cr
log off log2

// Run the first-stage regression group by group for the decile groups (Pre-Demonetization spending split by 10 groups)
eststo clear
forvalues i=1/10 {	
	eststo reg_noncash_grp`i': quietly reghdfe noncash_amt_pct after_cash_amt_pct_before ///
		if spending_grp10 == `i', ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
}

log on log2
display "Table 6 part 2"
#delimit ;	
estfe reg_noncash_grp* ,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_noncash_grp*, b(%8.3f) varwidth(40) brackets noconstant nostar
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations"))
	label  noomitted 
	indicate(`r(indicate_fe)')  nomtitles numbers   
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
	;
estfe reg_noncash_grp*, restore;
#delimit cr
log off log2

// After obtaining the subgroup-specific first-stage coefficient, we calculate 
// the relative likelihood by taking the ratio of the first-stage estimate for that subgroup to
// the overall first-stage estimate. This division calculations are done outside 
// of Stata.

* Table 7: Digital Payments and Different Spending Components
eststo clear
foreach type in food nonfood durable nondurable {
	eststo reg_ind_`type': reghdfe ind_`type' after_cash_amt_pct_before, ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
}

log on log2
display "Table 7"
#delimit ;	
estfe reg_ind_*,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_ind_food reg_ind_nonfood  
	reg_ind_durable reg_ind_nondurable, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)')
	nomtitles numbers  
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
	;
estfe reg_ind_*, restore;
#delimit cr
log off log2

* Table 8: Is Increased Spending Driven by the Shift to the Formal Market?
eststo clear
eststo reg_noncash_lowfood: reghdfe noncash_amt_pct after_cash_amt_pct_before if ind_high_food == 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_loglevel_lowfood: reghdfe ltender_amt_month after_cash_amt_pct_before if ind_high_food == 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_foodshare_lowfood: reghdfe share_food  after_cash_amt_pct_before if ind_high_food == 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
	
eststo reg_noncash_highfood: reghdfe noncash_amt_pct after_cash_amt_pct_before if ind_high_food == 1, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_loglevel_highfood: reghdfe ltender_amt_month after_cash_amt_pct_before if ind_high_food == 1, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_foodshare_highfood: reghdfe share_food  after_cash_amt_pct_before if ind_high_food == 1, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

log on log2
display "Table 8"
#delimit ;
estfe reg_*food,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_noncash_lowfood 
	reg_loglevel_lowfood 
	reg_foodshare_lowfood 
	reg_noncash_highfood 
	reg_loglevel_highfood 
	reg_foodshare_highfood , b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)') 
	nomtitles numbers   
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
;
estfe reg_*food, restore;
#delimit cr
log off log2

* Table 9: Is Increased Spending Driven by Income Shocks?
eststo clear
eststo reg_noncash_nouse: reghdfe noncash_amt_pct after_cash_amt_pct_before if cash4largebill == 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_loglevel_nouse: reghdfe ltender_amt_month after_cash_amt_pct_before if cash4largebill == 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_noncash_use: reghdfe noncash_amt_pct after_cash_amt_pct_before if cash4largebill == 1, ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant
eststo reg_loglevel_use: reghdfe ltender_amt_month after_cash_amt_pct_before if cash4largebill == 1, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

log on log2
display "Table 9"
#delimit ;
estfe reg_*_nouse reg_*_use,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_noncash_nouse reg_loglevel_nouse reg_noncash_use reg_loglevel_use, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)')
	nomtitles numbers 
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
	;
estfe reg_*_nouse reg_*_use, restore;
#delimit cr
log off log2

* Table 10: Is Increased Spending Driven by Credit Supply Shocks?
eststo clear
eststo reg_loglevel: reghdfe ltender_amt_month after_cash_amt_pct_before, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_existingcc: reghdfe ltender_amt_month after_cash_amt_pct_before ///
	if ~missing( credit_amt_pct_before) & credit_amt_pct_before > 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_existingcc2: reghdfe ltender_amt_month after_cash_amt_pct_before after_credit_amt_pct_before ///
	if ~missing( credit_amt_pct_before) & credit_amt_pct_before > 0, ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_cc_converted_user: reghdfe ltender_amt_month after_cash_amt_pct_before ///
	if ~missing( credit_amt_pct_before) & credit_amt_pct_before == float(0) & ~missing(credit_amt_pct_after) & credit_amt_pct_after > float(0), ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

eststo reg_cc_nonuser: reghdfe ltender_amt_month after_cash_amt_pct_before ///
	if ~missing( credit_amt_pct_before) & credit_amt_pct_before == float(0) & credit_amt_pct_after == float(0), ///
	absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) nosample noconstant

log on log2
display "Table 10"
#delimit ;	
estfe reg_loglevel reg_existingcc reg_existingcc2 reg_cc_converted_user reg_cc_nonuser,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_loglevel reg_existingcc reg_existingcc2 reg_cc_converted_user reg_cc_nonuser 
	, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)')
	nomtitles numbers  
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post" 
	after_credit_amt_pct_before "PriorCreditDependence * Post") 
	;
estfe reg_loglevel reg_existingcc reg_existingcc2 reg_cc_converted_user reg_cc_nonuser, restore;
#delimit cr
log off log2

* Table 12: Digital Payments and Spending on Temptation Goods
eststo clear
foreach type in temptation nontemptation {
	eststo reg_ind_`type': reghdfe ind_`type' after_cash_amt_pct_before, ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) noconstant
	
	eststo reg_ind`type'_low: reghdfe ind_`type' after_cash_amt_pct_before if inlist(spending_grp2,1), ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) noconstant
	
	eststo reg_ind`type'_high: reghdfe ind_`type' after_cash_amt_pct_before if inlist(spending_grp2,2), ///
		absorb(cid DTCode##yearmonth) vce(cluster cid  yearmonth) noconstant
}

log on log2
display "Table 12"
#delimit ;
estfe reg*temptation*,
	labels(cid "Individual FEs" yearmonth "Year-Month FEs" DTCode#yearmonth "District * Year-Month FEs");
esttab reg_ind_temptation 
	reg_ind_nontemptation
	reg_indtemptation_low 
	reg_indnontemptation_low
	reg_indtemptation_high 
	reg_indnontemptation_high, b(%8.3f) varwidth(40) brackets noconstant 
	stats(r2 N_full, fmt(a %15.0fc) labels("R2" "No. of Observations")) 
	label  noomitted 
	indicate(`r(indicate_fe)') 
	nomtitles numbers 
	varlabels(after_cash_amt_pct_before "PriorCashDependence * Post") 
;
estfe reg*temptation*, restore;
#delimit cr
log off log2
